package cool.tianyou.blue.bird.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import cool.tianyou.blue.bird.R;
import cool.tianyou.blue.bird.model.Log;
import cool.tianyou.blue.bird.util.LogUtil;

import java.text.SimpleDateFormat;
import java.util.List;


public class LogListAdapter extends BaseAdapter {

    private final List<Log> logs;

    private final LayoutInflater mLayoutInflate;
    private final Context context;

    public LogListAdapter(List<Log> logs, Context context) {
        this.logs = logs;
        mLayoutInflate = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public int getCount() {
        return logs.size();
    }

    @Override
    public Object getItem(int position) {
        return logs.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ViewHolder")
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position >= logs.size()) {
            return null;
        }
        Log log = logs.get(position);
        convertView = mLayoutInflate.inflate(R.layout.list_item_layout, parent, false);

        TextView tvContent = convertView.findViewById(R.id.tv_log_content);
        ImageView iv = convertView.findViewById(R.id.iv_icon);

        tvContent.setText(log.getContent());

        iv.setImageIcon(log.getIcon());
        if (log.getIcon() != null) {
            iv.setImageIcon(log.getIcon());
        }


        TextView tvTime = convertView.findViewById(R.id.tv_log_time);
        tvTime.setText(new SimpleDateFormat("MM-dd HH:mm:ss").format(log.getTime()));

        LinearLayout layout = convertView.findViewById(R.id.layout_lb_c);


        TextView labelView = convertView.findViewById(R.id.lb_device);
        TextView noDevice = convertView.findViewById(R.id.lb_no_device);
        TextView pushing = convertView.findViewById(R.id.lb_pushing);
        if(log.getPushedDevices()!=null){
            for (String pushedDevice : log.getPushedDevices()) {
                TextView lb1 = new TextView(context);
                lb1.setBackgroundResource(R.drawable.back);
                lb1.setTextColor(labelView.getTextColors());
                lb1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
                lb1.setPadding(labelView.getPaddingLeft(), labelView.getPaddingTop(), labelView.getPaddingRight(), labelView.getPaddingBottom());
                lb1.setLayoutParams(labelView.getLayoutParams());
                lb1.setBackground(labelView.getBackground());
                lb1.setText(pushedDevice);
                layout.addView(lb1);
            }
            if (!log.getPushedDevices().isEmpty()) {
                layout.removeView(noDevice);
            }
            layout.removeView(pushing);
        }else{
            layout.removeView(noDevice);
        }




        labelView.setVisibility(View.INVISIBLE);
        layout.removeView(labelView);
        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                LogUtil.logs.remove(log);
                LogListAdapter.super.notifyDataSetChanged();
                return false;
            }
        });

        return convertView;
    }


}
